package cn.ytr.crm.web.controller;

import cn.ytr.crm.domain.SystemDictionaryItem;
import cn.ytr.crm.query.SystemDictionaryItemQueryObject;
import cn.ytr.crm.service.ISystemDictionaryItemService;
import cn.ytr.crm.service.ISystemDictionaryService;
import cn.ytr.crm.util.JsonResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/systemDictionaryItem")
@Controller
public class SystemDictionaryItemController {

	@Autowired
	ISystemDictionaryItemService systemDictionaryItemService;
	@Autowired
	ISystemDictionaryService systemDictionaryService;

	/**
	 * 显示 字典明细列表页
	 */
	@RequiresPermissions(value = {"访问:字典明细列表页面", "systemDictionaryItem:list"}, logical = Logical.OR)
	@RequestMapping("/list")
	public String list(@ModelAttribute("qo") SystemDictionaryItemQueryObject qo, Model model) {
		//字典目录的数据共享
		model.addAttribute("dics", systemDictionaryService.listAll());
		//用分页插件
		model.addAttribute("pageInfo", systemDictionaryItemService.query(qo));
		return "systemDictionary/item";
	}

	/**
	 * 处理 去增加或编辑字典明细页提交的
	 */
	@RequiresPermissions(value = {"操作:新增或修改字典明细", "systemDictionaryItem:saveOrUpdate"}, logical = Logical.OR)
	@RequestMapping("/saveOrUpdate")
    @ResponseBody
	public JsonResult saveOrUpdate(SystemDictionaryItem systemDictionaryItem) {
		JsonResult jsonResult = new JsonResult();
		try {
			systemDictionaryItemService.saveOrUpate(systemDictionaryItem);
		    return jsonResult;
		} catch (Exception e) {
		    jsonResult.mark("服务器提示：操作字典明细出错");
		    e.printStackTrace();
		    return jsonResult;
		}

	}

	/**
	 * 处理 删除字典明细
	 */
    @ResponseBody
    @RequiresPermissions(value = {"操作:删除字典明细", "systemDictionaryItem:delete"}, logical = Logical.OR)
	@RequestMapping("/delete")
	public JsonResult delete(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
			systemDictionaryItemService.delete(id);
            return jsonResult;
        } catch (Exception e) {
            jsonResult.mark("服务器提示：删除字典明细操作出错");
            e.printStackTrace();
            return jsonResult;
        }

	}

}
